﻿using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

namespace CMS.Common.Validation
{
    public class EmailAttribute : RegularExpressionAttribute, IClientValidatable
    {
        public EmailAttribute()
            : base(@"^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$")
        {
        }

        public System.Collections.Generic.IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            var rule = new ModelClientValidationRegexRule(ErrorMessage, Pattern);
            rule.ErrorMessage = FormatErrorMessage(metadata.GetDisplayName());
            return new[] { rule };
        }
    }
}